מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

Similar documents
תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

תוכנה 1 סמסטר א' תשע"א

מבוא למדעי המחשב תרגול 10 Comparator, Comparable, Binary Trees

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

תור שימושים בעולם התוכנה

חומר עזר לבחינה במבוא למדעי המחשב // Indicates whether some other object is "equal to" // this one. boolean equals(object other)

מבוא למדעי המחשב 2018 תרגול 7

תרגול 6 רקורסיה ותכנות מונחה עצמים

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

תרגול 7 רשימות משורשרות, רקורסיית

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

Introduction to Computer Science II (CSI 1101) Midterm Examination

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

תוכנה 1 * לא בהכרח בסדר הזה

Distributed Systems Recitation 1. Tamim Jabban

1. ArrayList and Iterator in Java

Chapter 5 - A Simple Dynamic Linked List (Holding Strings)

Interfaces, collections and comparisons

מבוא למדעי המחשב תרגול 10 הממשקים Iterator, Iterable Binary trees

מבני נתונים תכנות מונחה עצמים מבני נתונים. מחלקות אבסטרקטיות חבילות packages סיכום הרשאות גישה wrappers ADT מערך דינמי מחסנית

C12a: The Object Superclass and Selected Methods

Distributed Systems Recitation 1. Tamim Jabban

IMPLEMENTING BINARY TREES

Linked Lists. Chapter 12.3 in Savitch

ממשק משתמש גרפי בעזרת SWT

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

תוכנה 1 * לא בהכרח בסדר הזה

הנכות 1 םוכיס לוגרת 14 1

(Constructor) public A (int n){ for (int i = 0; i < n; i++) { new A(i); } System.out.println("*"); }

Object-Oriented Programming Concepts

תוכנה 1 * לא בהכרח בסדר הזה

CS 455 Midterm Exam 2 Fall 2016 [Bono] November 8, 2016

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

: Advanced Programming Final Exam Summer 2008 June 27, 2008

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

הנכות 1 םוכיס לוגרת 13 1

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

מבוא לתכנות ב- JAVA תרגול 7

Inheritance (Part 5) Odds and ends

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

1) Consider the following code segment, applied to list, an ArrayList of Integer values.

Polymorphism. return a.doublevalue() + b.doublevalue();

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Practical Session - Heap

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CMPSCI 187 Midterm 1 (Feb 17, 2016)

EXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

LinkedList Implementation Mini-project intro

The Java Collections Framework and Lists in Java Parts 1 & 2

public class B { private int f = 0; public static void main(string[] args) { B b1 = new B(); B b2 = new B(); Object b3 = b1;


חומר עזר לבחינה מבוא למדעי המחשב

Page 1 / 3. Page 2 / 18. Page 3 / 8. Page 4 / 21. Page 5 / 15. Page 6 / 20. Page 7 / 15. Total / 100. Pledge:

ANSWER KEY. Study Guide. Completely fill in the box corresponding to your answer choice for each question.

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

תרגול 12. Standard Template Library כתיבת אלגוריתמים גנריים מצביעים חכמים

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

The class Object. Lecture CS1122 Summer 2008

Introduction to Computer Science II (CSI 1101)

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

Instance Method Development Demo

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

DM503 Programming B. Peter Schneider-Kamp.

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CS 1331 Exam 1 ANSWER KEY

מבוא למדעי המחשב השפעת השינוי על סטודנט הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות אם ברצוננו ששכר הלימוד לא יעלה על 2500.

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

Page 1 of 16. Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

ספרית התבניות הסטנדרטית (STL) כתיבת אלגוריתמים גנריים מצביעים חכמים. .vector. list iterator נכיר תחילה את האוסף הפשוט ביותר בספריה

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Linked List. ape hen dog cat fox. tail. head. count 5

Tema 5. Ejemplo de Lista Genérica

Task 2 What is printed out when the code is executed?

Chapter 13. Inheritance and Polymorphism. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

SSE3052: Embedded Systems Practice

סעיף ב )11 נקודות( public synchronized void advance(staircase staircase) throws InterruptedException staircase!

PASS4TEST IT 인증시험덤프전문사이트

CS 2334: Programming Structures and Abstractions: Exam 1 October 3, 2016

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CONTAİNERS COLLECTİONS

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

שאלה 1 מהו הפלט של התוכנית הבאה:

תרגול 9 מחלקות ואובייקטים

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform

CSCD 326 Data Structures I Stacks

Transcription:

מבוא למדעי המחשב 2017 תרגול 8 רשימה משורשרת כללית, Comparator

בתרגול היום. LinkedList בניית ההכללה מ- LinkIntList תרגול המבנה ושימושיו ממשקים: Comparator Sorted Linked List ל- LinkedList ע"י שימוש ב- Comparator

תזכורת: רשימה משורשרת רשימה המשורשרת היא אוסף איברים, בכל איבר מאוחסן מידע ברשימה. איבר ברשימה הינו מסוג Link וכן מצביע לאיבר הבא כל הרשימה נקראת LinkedList null Link Data Next

תזכורת: המחלקה Link public class Link { // ---------------------- fields ---------------------- private Object data; private Link next; // ---------------------- constructors ---------------------- public Link(Object data, Link next) { this.data = data; this.next = next; public Link(Object data) { this(data, null); public Link getnext() { public void setnext(link link) { public Object getdata() { public String tostring() { public boolean equals(object other) { 4

השיטה tostring של המחלקה Link public String tostring() { String output = ""; if (data!= null){ output = data.tostring(); return output; 5

תזכורת: המחלקה LinkedList public class LinkedList implements List{ // ---------------------- fields ---------------------- private Link first; private Link last; // ---------------------- constructors ---------------------- public LinkedList(){ first = null; last = null; // ---------------------- methods ---------------------- public boolean isempty() { public Object get(int index) { public String tostring() { public boolean equals(object other) { public int size() { public void add(object element) { public void add(int index,object element) { public boolean contains(object element) { 6

השיטה tostring של המחלקה LinkedList public String tostring() { String output = "<"; Link current = first; while (current!= null) { output = output + current.tostring()+ ","; current = current.getnext(); output = output.substring(0, output.length()-1) + ">"; return output; 7

דריסה )Overriding) ו"כלל הבצל" LinkedList Link Object first last Link Link Object data next Object Link String tostring() boolean equals(object other) String tostring() boolean equals(object other) : : : String tostring() String tostring() boolean equals(object other) boolean equals(object other) : : : 8

השיטה Link של המחלקה equals public boolean equals(object other) { boolean isequal = true; if (! (other instanceof Link)) isequal = false; else { Link otherlink = (Link) other; if (data == null){ if (otherlink.data!= null){ isequal = false; else{ isequal = data.equals(otherlink.data); return isequal; 9

LinkedList השיטה equals של המחלקה public boolean equals(object other) { boolean isequal = true; if (! (other instanceof LinkedList)) isequal = false; else { Link mycurrent = first; Link othercurrent = ((LinkedList) other).first; while (isequal & mycurrent!= null & othercurrent!= null) { isequal = mycurrent.equals(othercurrent); mycurrent = mycurrent.getnext(); othercurrent = othercurrent.getnext(); if (isequal) isequal = (mycurrent == null && othercurrent == null); return isequal; 10

הפסקה 11

המחלקה Student public class Student{ // ---------------------- fields ---------------------- private String name; private LinkedList grades; private int id; // ---------------------- constructors ---------------------- public Student(String studentname, int studentid) { name = studentname; id = studentid; grades = new LinkedList(); public void addgrade(integer grade) {grades.add(grade); public String getname() {return name public double calcavarage() { public String tostring() { public boolean equals(object other) { 12

ממשקים ממשק מייצג רעיון מופשט. הממשק )interface) הינו כלי למימוש עיקרון ההפרדה בין הכרזה למימוש. מבחינת המתכנת, ממשק הוא חוזה עבור הפונקציות שצריך למלא. ממשק קובע את הפונקציונאליות המשותפת לכל המחלקות המממשות אותו.

ממשקים הצהרה על ממשק: public interface <name> { <methods list> ממשקים מתארים שיטות )public( ללא מימושן. ממשקים אינם כוללים בנאים שכן אין דרך ליצר מופע )אובייקט( של ממשק אלא רק של מחלקה המממשת את הממשק. בדומה לשימוש במחלקות, המשתמש בממשק אינו צריך להכיר את פרטי המימוש של השיטות השונות.

הממשק Comparator public interface Comparator { public int compare(object obj1,object obj2); החוזה של השיטה אומר: יהיו a ו- b שני עצמים בני השוואה a > b compare(a,b) > 0 a.equals(b) compare(a,b) = 0 a < b compare(a,b) < 0 מימוש השיטה compare מגדיר את סוג ההשוואה המתבצעת 15

מימושים שונים לממשק Comparator public class StudentIDComparator implements Comparator { public int compare(object s1, Object s2) { if (! (s1 instanceof Student)!(s2 instanceof Student)) throw new RuntimeException(...); return ((Student) s1).getid()-((student) s2).getid(); public class StudentAverageComparator implements Comparator { public int compare(object s1, Object s2) { if (! (s1 instanceof Student)!(s2 instanceof Student)) throw new RuntimeException(...); double diff = ((Student) s2).calcavarage()-((student) s1). calcavarage(); if (diff > 0) return (int)(diff+1); return (int)diff;

הוספת findminvalue למחלקה LinkedList public Object findminvalue(comperator c){ Object minvalue = first.getdata(); Link currentlink = first; while (currentlink!= null){ if (c.compare(currentlink.getdata(), minvalue) < 0){ minvalue = currentlink.getdata() ; currentlink = currentlink.getnext(); return minvalue;

public Int id: static 1234 void main(string[] Object args) data: 92 Link first: { Student LinkedList grades: s1 = new Student( Dana,1234); Link next: s1.addgrade(92); Student s1.addgrade(56); String name: Yossi s1.addgrade(70); Int id: 5678 Link first: Student LinkedList s2 = grades: new Student( Yossi,5678); s2.addgrade(35); Student s2.addgrade(60); String name: Itay Student s3 = new Int id: Student( Itay,91011); Link first: s3.addgrade(100); LinkedList grades: s3.addgrade(98); Type Name Value Student Student Student Student String name: Dana s1 s2 s3 רשימה משורשרת של סטודנטים LinkedList Link Link Link Object data: 35 Link next: Object data: 56 Link next: LinkedList Link Link Object data: 100 Link next: Object data: 65 Link next: null Object data: 70 Link next: null LinkedList Link Link Object data: 98 Link next: null

רשימה משורשרת של סטודנטים public static void main(string[] Student args) {... String name: Dana LinkedList studentlist = new Int id: LinkedList(); 1234... studentlist.add(s1); LinkedList grades: studentlist.add(s2); studentlist.add(s3); Type Name Value Student Student Student LinkedList s1 s2 s3 studentlist Student String name: Yossi Int id: 5678 LinkedList grades:... Student String name: Itay Int id: 91011 LinkedList grades:... LinkedList Link Link Link Object data: Object data: Object data: Link first: Link next: Link next: Link next: null 19

החזרת המינימום ברשימה משורשרת public static void main(string[] args) { LinkedList studentlist = new LinkedList(); Comparator idcomp = new StudentIDComparator(); Comparator averagecomp = new StudentAverageComparator(); Object o1 = studentlist.findminvalue(idcomp); Object o2 = studentlist.findminvalue(averagecomp); Reference Type Name Instance Type value Comparator idcomp StudentIDComparator Comparator averagecomp StudentAverageComparator Object o1 Student Object o2 Student String name: Dana Int id: 1234 LinkedList grades: String name: Itay Int id: 91011 LinkedList grades:

השיטה tostring של המחלקה Student public String tostring() { return [ + Name: +name +, ID: + id +, Grades: + grades + ] ; public static void main(string[] args) { Student s = new Student( Dana,1234); s.addgrade(92); s.addgrade(56); s.addgrade(70); System.out.println(s); [Name: Dana, ID: 1234, Grades: <92, 56, 70>] 21

רשימה משורשרת של סטודנטים public static void main(string[] args) { Student s1 = new Student( Dana,1234); s1.addgrade(92); s1.addgrade(56); s1.addgrade(70); Student s2 = new Student( Yossi,5678); s2.addgrade(35); s2.addgrade(60); Student s3 = new Student( Itay,91011); s3.addgrade(100); s3.addgrade(98); LinkedList studentlist = new LinkedList(); studentlist.add(s1); studentlist.add(s2); studentlist.add(s3); System.out.println(studentList); <[Name: Dana, ID: 1234, Grades: <92, 56, 70>], [Name: Yossi, ID: 5678, Grades: <35, 60>], [Name: Itay, ID: 91011, Grades: <100, 98>]> 22